package cn.hicard.risk.web;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import cn.hicard.risk.common.Constant;
import cn.hicard.risk.entity.BdCallorder;
import cn.hicard.risk.service.bd.CallorderService;
import cn.hicard.util.AppConfig;
import cn.hicard.util.DateHelp;
import cn.hicard.util.SpringContext;

public class CallorderAction {

	// 日志工具
	private static Logger logger = Logger.getLogger(Constant.LOGGER_MAIN);
	
	// 调单业务类
	private CallorderService callorderService;
	
	public void setCallorderService(CallorderService callorderService) {
		this.callorderService = callorderService;
	}

	public void execute(HttpServletRequest request,
						 HttpServletResponse response) 
		throws Exception 
	{
		// 根据商议好的内容解析XML
		// 读取输入流的数据
		StringBuffer sbXml = new StringBuffer();
		BufferedReader reader = null;
		String charset = AppConfig.get("callorder_charset");
		try{
			reader = new BufferedReader(new InputStreamReader(request.getInputStream(), charset));
			String line = null;		
			while((line = reader.readLine()) != null){
				sbXml.append(line);
			}
			String xml = sbXml.toString();
			logger.info("调单接收的XML内容：" + xml);
			
			// 解析XML数据
			Document doc = DocumentHelper.parseText(xml);
			Element root = doc.getRootElement();			
			@SuppressWarnings("unchecked")
			List<Element> list = root.elements();
			HashMap<String, String> attributeMap = new HashMap<String, String>();			
			for (Element lmt : list) {				
				String key = lmt.getName();
				String value = lmt.getText();
				attributeMap.put(key, value);
			}
			
			// 将数据组装为调单对象
			BdCallorder callorder = new BdCallorder();
		   // <bankNo>收单行</bankNo>
			callorder.setBankno(attributeMap.get("bankno"));
		   // <isreck>是否清算</isreck>
			callorder.setIsreck(attributeMap.get("isreck"));
		   // <name>商户名称</name>
			callorder.setMerchname(attributeMap.get("merchname"));
		   // <id>商户号</id>
			callorder.setMerchno(attributeMap.get("merchno"));
		   // <hkmerchantno>汇卡商户号</hkmerchantno>			
		   // <merchName>商户简称</merchName>
		   //无用callorder.setMerchshortname(attributeMap.get("merchshortname"));
		   // <orderno>订单号</orderno>
			callorder.setOrderno(attributeMap.get("orderno"));
		   // <trans_date>交易日期时间</trans_date>
			String transdate = attributeMap.get("transdate");
			if(transdate != null){
				transdate = transdate.trim().replace("-", "");
			}
			callorder.setTransdate(transdate);				
			String transtime = attributeMap.get("transtime");
			if(transtime != null){
				transdate = transdate.trim().replace(":", "");
			}
			callorder.setTranstime(transtime);
		   // <cardno>信用卡号</cardno>
			callorder.setCardno(attributeMap.get("cardno"));
		   // <transamount>金额</transamount>
			callorder.setTransmoney(attributeMap.get("transamount"));
		   // <dispose>处理时间</dispose>				
		   // <operator>处理人</operator>
		   // <authid>审核结果</authid>
		   // <newamount>审核原因</newamount>
		   // <termNo>终端号</termNo>
			callorder.setTermno(attributeMap.get("termno"));			
			// 数据类型为金掌柜
			callorder.setDatatype("jzg");
			// 审核状态为未处理
			callorder.setCheckstate("0");
			callorder.setCreatedate(DateHelp.dateTime());
			
			// 调单图片
			callorder.setPicname("jzg"+callorder.getOrderno() + ".jpg");
			
			// 保存到数据库
			callorderService = (CallorderService)SpringContext.getBean("callorderService");
			// 验证是否已接收过该数据
			if(callorderService.existJzg(callorder.getOrderno())){
				logger.warn("订单号为" + callorder.getOrderno() + "的金掌柜调单数据已经存在,为防止数据重复放弃保存");
			}else{
				callorderService.save(callorder);
			}			
		}finally{
			if(reader != null){
				reader.close();
			}
		}
	}

}
